﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace TextControl.Controllers
{
    public class TextController : Controller
    {
        //
        // GET: /Text/
        [HttpGet]
        public ActionResult Index()
        {
           // ViewData["foo"] = "get";
            return View();
        }
        /*
        [HttpPost]
        public ActionResult Index(string uiTextarea)
        {
            //string text =Request.js
            var j = new { s = "text" };

            return Json(j, JsonRequestBehavior.AllowGet);
        }*/
        /*
        class Person
        {
            public string name { get; set; }
            public string text { get; set; }
        }*/
        /*
        [Serializable]
        public class TextareaInputModel
        {
            public string text { get; set; }
        }


        [HttpPost]
        public ActionResult Index(TextareaInputModel textareaInputModel)
        {
            var tmp = new { s = textareaInputModel.text };

            return Json(tmp, JsonRequestBehavior.AllowGet);
        }*/


        [HttpPost, ValidateInput(false)]
        public ActionResult GetResult(string text)
        {

            string acceptable = "h1|h2|h3|h4|h5|h6|em|strong|p|ol|ul|li";
            string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-z,A-Z,0-9]+)(?:\s[a-z,A-Z,0-9,\-]+=?(?:(["",']?).*?\1?))*\s*/?>";
            //string result = Regex.Replace(text, stringPattern, "");

            string res = Regex.Replace(text, stringPattern, x => Server.HtmlEncode(x.Value));

            var tmp = new { response = res };
            return Json(tmp, JsonRequestBehavior.AllowGet);
        }

    }
}
